package cn.stylefeng.guns.modular.User.service.Impl;



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.modular.User.entity.User;
import cn.stylefeng.guns.modular.User.enums.UserEnum;
import cn.stylefeng.guns.modular.User.mapper.UserMapper;
import cn.stylefeng.guns.modular.User.param.UserParam;
import cn.stylefeng.guns.modular.User.service.UserService;
import cn.stylefeng.guns.sys.modular.emp.service.SysEmpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

// 服务实现类
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private SysEmpService sysEmpService;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> list(UserParam userParam) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(User::getStatus, CommonStatusEnum.DELETED.getCode());

        return this.list(queryWrapper);
    }

    @Override
    public void add(UserParam userParam) {
        User user = new User();
        BeanUtil.copyProperties(userParam, user);
        this.save(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(UserParam userParam) {
        User user = this.queryCanteenUser(userParam);


        user.setStatus(CommonStatusEnum.DELETED.getCode());
        this.updateById(user);
        Long id = user.getId();
        //删除该用户对应的员工表信息
        sysEmpService.deleteEmpInfoByUserId(id);

        this.removeById(id);

    }

    @Override
    public void edit(UserParam userParam) {
        User user = this.queryCanteenUser(userParam);
        BeanUtil.copyProperties(userParam, user);
        user.setStatus(null);
        this.updateById(user);
    }

    @Override
    public List<User> lookWaiterScore(int pid, int day) {
        List<User> list = userMapper.lookWaiterSort(pid,day);

        return list;
    }

    @Override
    public void changeStatus(UserParam userParam) {
        Long id = userParam.getId();
        Integer status = userParam.getStatus();

        // 校验状态在不在枚举值里
        CommonStatusEnum.validateStatus(status);

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(User::getId,id)
                .and(i->i.ne(User::getStatus,CommonStatusEnum.DELETED.getCode()))
                .set(User::getStatus,status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }

    }
    // 获取UserWaiter
    private User queryCanteenUser(UserParam userParam) {
        User user = this.getById(userParam.getId());
        if (ObjectUtil.isEmpty(user)) {
            throw new ServiceException(UserEnum.NOT_EXISTED);
        }
        return user;
    }

}
